package com.teamview.view;

import com.teamview.domain.Employee;
import com.teamview.domain.Programmer;
import com.teamview.service.NameListService;
import com.teamview.service.TeamException;
import com.teamview.service.TeamService;

/**
 * @author Lenl
 * @create 2021-06-02 12:22
 */
public class TeamView {
    private NameListService listSvc=new NameListService();
    private TeamService teamSvc=new TeamService();
    public void enterMainMenu(){

        boolean flag=true;
        char menu=0;
        while(flag){
            if(menu!='1')
            listAllEmployees();
            System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 (请选择1-4)：");
            menu= TSUtility.readMenuSelection();
            switch(menu){
                case '1':
                    getTeam();
                    break;
                case '2':
                    addMember();
                    break;
                case '3':

                    deleteMember();
                    break;
                case  '4':
                    System.out.print("是否退出(y/n):");
                    char isExit=TSUtility.readConfirmSelection();
                    if(isExit=='Y'){
                        flag=false;
                        System.out.println("程序已退出...");
                    }
                    break;
            }
        }

    }

    private void listAllEmployees(){
        System.out.println("---------------------------开发团队调度软件---------------------------\n");
        System.out.println("ID\t员工名\t年龄\t工 资\t\t职位\t\t状态\t\t奖 金\t\t股票\t\t领用设备");
        Employee[] employees = listSvc.getAllEmployees();
        if(employees.length==0){
            System.out.println("当前公司无员工！");
        }else{
            for(int i=0;i<employees.length;i++){
                System.out.println(employees[i].toString());
            }
        }
        System.out.println("----------------------------------------------------------------------------------\n");

    }

    private void getTeam(){
        System.out.println("---------------------------团队成员列表---------------------------\n");
        Programmer[] programmers= teamSvc.getTeam();
        if(programmers==null|| programmers.length==0){
            System.out.println("开发团队没有成员");
        }else{
            System.out.println("TID/ID\t员工名\t年龄\t\t工 资\t职位\t\t奖 金\t股票");
            for (int i=0;i<programmers.length;i++){
                System.out.println(programmers[i].getDetailsForTeam());
            }
        }

        System.out.println("----------------------------------------------------------------------------------\n");

    }
    private void addMember(){
        System.out.println("---------------------------添加成员---------------------------\n");
        System.out.print("请输入要添加的员工ID：");
        int id=TSUtility.readInt();

        try {
            Employee emp=listSvc.getEmployee(id);
            teamSvc.addMember(emp);
            System.out.println("添加成功！");

        } catch (TeamException e) {
            System.out.println(e.getMessage());
        }
        //按回车键继续
        TSUtility.readReturn();

    }
    private void deleteMember(){
        System.out.println("---------------------------删除成员---------------------------\n");
        System.out.print("请输入要删除员工的id：");
        int memberId=TSUtility.readInt();
        System.out.print("确认是否删除(y/n)：");
        char isDelete= TSUtility.readConfirmSelection();
        if(isDelete=='N'){
            return;
        }
        try {
            teamSvc.removeMember(memberId);
            System.out.println("删除成功！");
        } catch (TeamException e) {
            System.out.println("删除失败，原因："+e.getMessage());
            System.out.print("按回车继续...");
            TSUtility.readReturn();
        }
    }

    public static void main(String[] args) {
        TeamView view=new TeamView();
        view.enterMainMenu();

    }
}
